/**
 * Created by zzy on 2016/12/8.
 */
var $ = require('jquery');
require('easyui');
require('layout/uiJs');
var $alert = require("libs/alert");
var $confirm = require("libs/confirm");
require('js-path/jquery-validation-wrapper');
var isSale = require('libs/user-type').isSale;
$(function () {
    $('#ft').show();

    //退货单修改
    var validator = $('#mModifyForm').validateWapper({
        rules: {
            refundWayNo:{
                required: true
            }
        },
        submitHandler: function() {
            var formData = $('#mModifyForm').serializeObject();
            $.ajax({
                url: 'api/v1/p/mms/refund/update',
                type: 'post',
                data: JSON.stringify(formData),
                contentType: 'application/json;charset=utf-8'
            })
                .done(function (res) {
                    if(res.code =='200'){
                        $("#modifyModal").modal("hide");
                        $("#refundTable").datagrid("reload");
                    }else{
                        $alert(res.message);
                    }
                });
        }
    });
    $('#modifyModal').on('show.bs.modal',function (e) {
        var button = $(e.relatedTarget);
        var id = button.data('id');
        $('input[name=id]',this).val(id);
    });
    $("#modifyModal").on("hidden.bs.modal",function () {
        $('input',this).val('');
        validator.resetForm();
    });
    $('#mModifySubmit').on('click',function () {
       $('#mModifyForm').submit();
    });
    //退货单修改end

    //退货单编号查询
    $('#mRefundSearch').on('click',function () {
       var $tbody = $('#mRefundSearchForm').find('table tbody'),
           formData = $('#mRefundSearchForm').serializeObject();
       if(formData.refundNo.trim() == ''){
           $tbody.empty().append("<tr><td colspan='2' style='text-align: center;color: red;'>请先输入有效数据</td></tr>");
           return false;
       }
       $.get('api/v1/p/mms/refund/queryRefundNo',formData,function (res) {
           if(res.code =='200'){
               var data = res.data;
               if(data == ""||data.toString() == ""){
                   $tbody.empty().append("<tr><td colspan='2' style='text-align: center;color: red;'>暂无数据</td></tr>");
                   return false;
               }
               var td1 = data.entName;
               var td2;
               switch (data.status){
                   case 'unsaved':
                       td2 = '未保存';break;
                   case 'uncommit':
                       td2 = '未提交';break;
                   case 'commit':
                       td2 = '已提交';break;
                   case 'recieved':
                       td2 = '已收货';break;
                   case 'processed':
                       td2 = '已处理';break;
                   case 'processing':
                       td2 = '处理中';break;
                   case 'shelveing':
                       td2 = '上架中';break;
                   case 'invalid':
                       td2 = '已取消';break;
                   default: td2 = '';break;
               }
               $tbody.empty().append("<tr><td>"+td1+"</td><td>"+td2+"</td></tr>");
           }else{
               $alert(res.message);
           }
       })
    });
    $("#refundSearchModal").on("hidden.bs.modal",function () {
        $('input',this).val('');
        $('#mRefundSearchForm').find('table tbody').empty();
    });
    //退货单编号查询end

    //退货单删除
    $(document).on('click','a[name=invalid]',function () {
        var id = $(this).data('id');
        var arr = [];
        arr.push({id: id});
        $confirm("确认作废该退货单吗？",function () {
            $.ajax({
                url: 'api/v1/p/mms/refund/delete',
                type: 'post',
                contentType: "application/json; charset=utf-8",
                data:JSON.stringify(arr),
                dataType: 'json'
            })
                .done(function (res) {
                    if(res.code=='200'){
                        $("#refundTable").datagrid("reload");
                    }else{
                        $alert(res.message);
                    }
                })
        })
    });
    //退货单删除end

    $(document).on('click','a[name=detail]',function () {
        var id = $(this).data('id');
        window.location.href = "refund-detail?id="+id;
    });
    $(document).on('click','a[name=orderDetail]',function () {
        window.parent.tabOpen($(this).attr('ref'),'出库单详情',true,true);
    });
    $('#search').on('click',function () {
        var formData = $('#searchForm').serializeObject();
        formData.dateFrom = $('#startDt').data('utc0');
        formData.dateTo = $('#endDt').data('utc0');
        $('#refundTable').datagrid('load',formData);
    });
    warehouseInit();
    tableInit();
    function warehouseInit() {
        $('select[name=warehouseId]').combobox({
            url:'api/v1/p/crm/warehouse/query',
            valueField:'warehouseId',
            textField:'warehouseName',
            queryParams:{
                warehouseType:'DEST'
            },
            loadFilter:function(result){
                if(result.code=='200'){
                    result.data.unshift({warehouseId:'',warehouseName:'全部'});
                    return result.data;
                }
            }
        })
    }
    function tableInit() {
        $('#refundTable').datagrid({
            url:'api/v1/p/mms/refund/list',
            border : false,
            height:$(window).height(),
            pagination : true,
            pageSize : 20,
            pageList : [ 20, 50,  100 ],
            emptyMsg : "暂无数据",
            singleSelect: true,
            rownumbers:true,
            columns:[[
                {
                    field:'refundNo',
                    title:'退货单编号',
                    width:'8%',
                    formatter:function (value,row,index) {
                        return "<a class='ellipsis' href='javascript:;' name='detail' data-id='"+row.id+"' title='"+value+"'>"+value+"</a>";
                    }
                },{
                    field:'entName',
                    title:'客户名称',
                    width:'8%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'orderNo',
                    title:'关联订单号',
                    width:'10%',
                    formatter:function (value,row,index) {
                        return "<a class='ellipsis' href='javascript:;' ref='out-order-detail?id="+row.outroomOrderId+"' name='orderDetail' data-id='"+row.outroomOrderId+"' title='"+value+"'>"+value+"</a>";
                    }
                },
                {
                    field:'refundWayNo',
                    title:'退货运单号',
                    width:'12%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },
                {
                    field:'createType',
                    title:'退件类型',
                    width:'8%',
                    formatter:function (value,row,index) {
                        var ret = '';
                        if(value === 'self_care'){
                            ret = '退货自理';
                        }else if(value === 'return_mail'){
                            ret = '回邮单退货';
                        }else if(value === 'O_return'){
                            ret = '订单回退';
                        }
                        return ret;
                    }
                },{
                    field:'warehouseName',
                    title:'海外仓',
                    width:'10%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'skuNums',
                    title:'SKU数量',
                    width:'8%'
                },{
                    field:'goodsNums',
                    title:'商品数量',
                    width:'8%'
                },{
                    field:'submitDt',
                    title:'提交时间',
                    width:'13%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'status',
                    title:'退货单状态',
                    width:'8%',
                    formatter:function (value,row,index) {
                        switch (value){
                            case 'commit':
                                return '已提交';
                            case 'recieved':
                                return '已收货';
                            case 'processed':
                                return '已处理';
                            case 'processing':
                                return '处理中';
                            case 'shelveing':
                                return '上架中';
                            case 'invalid':
                                return '已取消';
                            default: return '';
                        }
                    }
                },{
                    field:'opt',
                    title:'操作',
                    hidden:isSale,
                    width:'7%',
                    formatter:function (value,row,index) {
                        var modifyButton = "<a href='javascript:;' data-toggle='modal' data-target='#modifyModal' data-id='"+row.id+"'>修改</a>";
                        var cancelButton = "<a href='javascript:;' name='invalid' data-id='"+row.id+"'>作废</a>";
                        if(row.status == 'commit'){
                            return modifyButton+'|'+cancelButton;
                        }else{
                            return '';
                        }
                    }
                }
            ]],
            loadFilter:function(data){
                if(data.code == '200'){
                    return data.data;
                }else{
                    return [];
                }
            },
            toolbar:"#tb"
        });
    }
});
